package org.jeecg.modules.demo.socket.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.jeecg.modules.demo.socket.entity.ZyAppDyna;
import org.jeecg.modules.demo.socket.mapper.ZyAppDynaMapper;
import org.jeecg.modules.demo.socket.service.PushService;
import org.jeecg.modules.demo.socket.until.NettyConfig;
import org.springframework.stereotype.Service;

import java.util.concurrent.ConcurrentHashMap;

@Service
public class PushServiceImpl extends ServiceImpl<ZyAppDynaMapper, ZyAppDyna> implements PushService {

    /*
    ConcurrentHashMap是属于JUC工具包中的并发容器之一，在多线程开发中很经常会使用到这个类，
    它与HashMap的区别是HashMap是线程不安全的，在高并发的情况下，使用HashMap进行大量变更
    操作容易出现问题，但是ConcurrentHashMap是线程安全的。 JDK1.8的实现已经抛弃了Segment
    分段锁机制，利用CAS+Synchronized来保证并发更新的安全，采用的数据结构（数组+链表+红黑树）。
     ConcurrentHashMap 是设计为非阻塞的。在更新时会局部锁住某部分数据，但不会把整个表都锁住。
     同步读取操作则是完全非阻塞的。好处是在保证合理的同步前提下，效率很高。
     ConcurrentHashMap的作用与用法:
     https://blog.csdn.net/qq_37488998/article/details/109715821#:~:text=ConcurrentHashMap%E7%9A%84%E4%BD%9C%E7%94%A8%E4%B8%8E%E7%94%A8%E6%B3%95.%20%E4%B8%80.ConcurrentHashMap%E7%AE%80%E4%BB%8B.%20ConcurrentHashMap%E6%98%AF%E5%B1%9E%E4%BA%8EJUC%E5%B7%A5%E5%85%B7%E5%8C%85%E4%B8%AD%E7%9A%84%E5%B9%B6%E5%8F%91%E5%AE%B9%E5%99%A8%E4%B9%8B%E4%B8%80%EF%BC%8C%E5%9C%A8%E5%A4%9A%E7%BA%BF%E7%A8%8B%E5%BC%80%E5%8F%91%E4%B8%AD%E5%BE%88%E7%BB%8F%E5%B8%B8%E4%BC%9A%E4%BD%BF%E7%94%A8%E5%88%B0%E8%BF%99%E4%B8%AA%E7%B1%BB%EF%BC%8C%E5%AE%83%E4%B8%8EHashMap%E7%9A%84%E5%8C%BA%E5%88%AB%E6%98%AFHashMap%E6%98%AF%E7%BA%BF%E7%A8%8B%E4%B8%8D%E5%AE%89%E5%85%A8%E7%9A%84%EF%BC%8C%E5%9C%A8%E9%AB%98%E5%B9%B6%E5%8F%91%E7%9A%84%E6%83%85%E5%86%B5%E4%B8%8B%EF%BC%8C%E4%BD%BF%E7%94%A8HashMap%E8%BF%9B%E8%A1%8C%E5%A4%A7%E9%87%8F%E5%8F%98%E6%9B%B4%E6%93%8D%E4%BD%9C%E5%AE%B9%E6%98%93%E5%87%BA%E7%8E%B0%E9%97%AE%E9%A2%98%EF%BC%8C%E4%BD%86%E6%98%AFConcurrentHashMap%E6%98%AF%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E7%9A%84%E3%80%82.,JDK1.8%E7%9A%84%E5%AE%9E%E7%8E%B0%E5%B7%B2%E7%BB%8F%E6%8A%9B%E5%BC%83%E4%BA%86Segment%E5%88%86%E6%AE%B5%E9%94%81%E6%9C%BA%E5%88%B6%EF%BC%8C%E5%88%A9%E7%94%A8CAS%2BSynchronized%E6%9D%A5%E4%BF%9D%E8%AF%81%E5%B9%B6%E5%8F%91%E6%9B%B4%E6%96%B0%E7%9A%84%E5%AE%89%E5%85%A8%EF%BC%8C%E9%87%87%E7%94%A8%E7%9A%84%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%EF%BC%88%E6%95%B0%E7%BB%84%2B%E9%93%BE%E8%A1%A8%2B%E7%BA%A2%E9%BB%91%E6%A0%91%EF%BC%89%E3%80%82.%20ConcurrentHashMap%20%E6%98%AF%E8%AE%BE%E8%AE%A1%E4%B8%BA%E9%9D%9E%E9%98%BB%E5%A1%9E%E7%9A%84%E3%80%82.
     */

    @Override
    public void pushMsgToOne(String userId){
        ConcurrentHashMap<String, Channel> userChannelMap = NettyConfig.getUserChannelMap();
        Channel channel = userChannelMap.get(userId);
        channel.writeAndFlush(new TextWebSocketFrame("有新消息推送，请查看"));
    }
    @Override
    public void pushMsgToAll(String msg){
        NettyConfig.getChannelGroup().writeAndFlush(new TextWebSocketFrame(msg));
    }
}
